// pages/zhanghu/shouhuodz/shouhuodz.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
 useData:"",
 show:0
  },
  chooAdd: function (res) {
    console.log(res.currentTarget.dataset.addrid)
    console.log(res.currentTarget.dataset.index)
    app.address.addrId = res.currentTarget.dataset.addrid
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/address/changeDefault ',
      method: 'POST',
      data: {
        addrId: res.currentTarget.dataset.addrid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log(res)
        wx.navigateBack({
          delta: 1
        })

      }
    })
  },
  setDefault: function (res) {
    var that = this
    console.log(res)
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/address/changeDefault ',
      method: 'POST',
      data: {
        addrId: res.currentTarget.dataset.addrid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log(res)
        console.log("设为默认成功")
        wx.showToast({
          title: '设为默认成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) {        
            that.onShow()
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
  },
  setAddress: function () {
    wx.navigateTo({
      url: '../shouhuodz/xinzengdz/xinzengdz?i=1',
    })
  },
  editAdd: function (res) {
    console.log(res)
    app.address.addrId = res.currentTarget.dataset.addrid
    wx.navigateTo({
      url: '../shouhuodz/bianji/bianji?i=1' + '&addrId=' + res.currentTarget.dataset.addrid,
    })
  },
  delAdd: function (res) {
    var that=this;
    console.log(res)
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/address/delete',
      method: 'POST',
      data: {
        addrId: res.currentTarget.dataset.addrid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log(res)
        console.log("删除成功")

        wx.showToast({
          title: '删除地址成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) {
            // wx.redirectTo({
            //   url: '../shouhuodz/shouhuodz',
            // })
            that.onShow()
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/address/all',
      method: 'POST',
      data: {
        userId: app.globalData.userId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log(res)
        this.setData({
          useData: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})